/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import bean.Autor;
import bean.Noticia;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jorgepereira
 */
public class NoticiaDAO implements Serializable {
    
    
    private Conexao conexao;
    private PreparedStatement ps;
    ResultSet rs;
    private boolean sucess = false;

    public NoticiaDAO() {
        this.conexao = new ConexaoPostgres();
    }
    
    
    
    public boolean cadastrar(Noticia noticia){
        
        
        
        String insert = "INSERT INTO noticia( resumo, texto, titulo, imagem, data) VALUES (?, ?, ?, ?, now());";
        
         sucess = false;

        try {

            ps = conexao.getConnection().prepareStatement(insert);
            ps.setString(1, noticia.getResumo());
            ps.setString(2, noticia.getTexto());
            ps.setString(3, noticia.getTitulo());
            if (noticia.getImagem().getConteudo() != null) {
                System.out.println("NoticiaDao -> Cadastrar: Imagem não é nula");
                int idImg = new ImagemDao().cadastrarImagem(noticia.getImagem());
                ps.setInt(4, idImg);
            } else {
                System.out.println("NoticiaDao -> Cadastrar: Imagem é nula");
                ps.setNull(4, Types.NULL);
            }
            
            ps.executeUpdate();
            conexao.closeConnection();


            sucess = true;
        } catch (SQLException ex) {
            conexao.closeConnection();
            Logger.getLogger(InstituicaoDao.class.getName()).log(Level.SEVERE, null, ex);
            return sucess;
        }

        conexao.closeConnection();
        return sucess;
        
        
    }
    
    public Noticia buscarNoticia(int id){
        
        Noticia noticia = new Noticia();
        
        String query = "Select * from noticia where id = ?";
        
        try {
            ps = conexao.getConnection().prepareStatement(query);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            
            while(rs.next()){
                noticia.setId(rs.getInt("id"));
                noticia.setData(rs.getDate("data"));
                noticia.getImagem().setId(rs.getInt("imagem"));
                noticia.setResumo(rs.getString("resumo"));
                noticia.setTexto(rs.getString("texto"));
                noticia.setTitulo(rs.getString("titulo"));
            }
            
            
        } catch (SQLException ex) {
            Logger.getLogger(NoticiaDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return noticia;
        
    }
    
    
    public List<Noticia> buscarDestaques(){
        ArrayList<Noticia> destaques = new ArrayList();
        
        String query = "select id, resumo, titulo from noticia  order by id  desc LIMIT 3";
        
        try {
            ps = conexao.getConnection().prepareStatement(query);
            rs = ps.executeQuery();
            
            while(rs.next()){
                Noticia n = new Noticia();
                n.setId(rs.getInt("id"));
                n.setResumo(rs.getString("resumo"));
                n.setTitulo(rs.getString("titulo"));
                destaques.add(n);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(NoticiaDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
        return destaques;
    }
    
}
